import { _decorator, Component, Vec3 } from 'cc';
import { InputBridge } from '../input/InputBridge';
import { AutoReloadSystem } from '../systems/AutoReloadSystem';

const { ccclass, property } = _decorator;

@ccclass('PlayerController')
export class PlayerController extends Component {
  @property(InputBridge)
  public inputBridge: InputBridge | null = null;

  @property(AutoReloadSystem)
  public autoReload: AutoReloadSystem | null = null;
  
  @property
  public movementSpeed = 4; // units per second (debug move)

  update(dt: number) {
    if (!this.inputBridge || !this.autoReload) {
      return;
    }
    const snapshot = this.inputBridge.snapshot();
    this.autoReload.setSpeedNorm(snapshot.move.length());

    // Debug movement for visible feedback in demo scene
    if (snapshot.move.lengthSqr() > 0) {
      const dir = new Vec3(snapshot.move.x, 0, snapshot.move.y);
      if (dir.lengthSqr() > 0) {
        dir.normalize();
        const wp = this.node.worldPosition.clone();
        wp.add3f(dir.x * this.movementSpeed * dt, 0, dir.z * this.movementSpeed * dt);
        this.node.setWorldPosition(wp);
      }
    }
  }
}
